package com.mrjy.share.bo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.mrjy.common.dto.SMSSendDto;
import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.SMSSendReqInfo;
import com.mrjy.common.entity.ShareMemberInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ResponseCode;
import com.mrjy.common.util.SMSTemplate;
import com.mrjy.common.util.SMSUtil;
import com.mrjy.maintain.dao.ISMSSendInfoDao;
import com.mrjy.share.dao.IShareMemberDao;


@Service("shareMemberBo")
public class ShareMemberBo implements IShareMemberBo {
	
	@Resource
	private IShareMemberDao shareMemberDao;
	
	@Resource
	private ISMSSendInfoDao smsSendInfoDao;

	@Override
	public List<ShareMemberInfo> listShareCommisionInfo(ShareMemberInfo sm) {
		List<ShareMemberInfo> smList =shareMemberDao.listShareUserInfo(sm);
		for(ShareMemberInfo s: smList){
			int total=shareMemberDao.countShareMemberByUserId(s.getUserId());
			s.setRecommendNum(total); 
			s.setShareProfit(total*100);
		}
		return smList;
	}


	@Override
	public ShareMemberInfo shareCommisionByUserId(ShareMemberInfo sa) {
		int total = shareMemberDao.countShareMemberByUserId(sa.getUserId());
		sa.setRecommendNum(total);
		sa.setShareProfit(total * 100);
		
		if(null == sa.getFirstSettlement()){
			sa.setFirstSettlement(0);//0 一级分销未结算  1 一级分销已经结算
		}
		int unSettlementTotal = shareMemberDao.countShareMemberByUser(sa);
		sa.setUnSettleMent(unSettlementTotal * 100);
		return sa;
	}


	@Override
	public Object listShareMembersByUserId(ShareMemberInfo sa) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("getFirstSettleMent"+sa.getFirstSettlement());
		List<ShareMemberInfo> smList = shareMemberDao.listShareMembersByUserId(sa);
		int total=shareMemberDao.countShareMemberByUserId(sa.getUserId());
		map.put("rows", smList);
		map.put("total", total);
		return map;
	}

	/**
	 * 月底统计分销商的推荐人数，并发送短信通知
	 *
	 */
	@Override
	public void countShareMember() {
		//查询一级和二级分销
		List<ShareMemberInfo> smList =shareMemberDao.listShareUserInfo2();
		//统计每一个分销推荐的会员总数并发送短信通知
		for(ShareMemberInfo s: smList){
			int total;
			if(s.getShareLevel()==1){
				 total=shareMemberDao.countShareMemberByUserId(s.getUserId());
				s.setRecommendNum(total); 
				//s.setShareProfit(total*100);
			}else{
				ShareMemberInfo sm=new ShareMemberInfo();
				sm.setShareCode(s.getShareCode());
				 total=shareMemberDao.countShareMemberInfo(sm);
				s.setRecommendNum(total); 
				//s.setShareProfit(total*100);
			}
			if(total>0){
				SMSSendDto smsSendDto=new SMSSendDto();
				smsSendDto.setName(s.getUserName());
				smsSendDto.setRecNum(s.getMobilePhone());
				Calendar cale = Calendar.getInstance();
				cale.add(Calendar.DATE, -1);  
				smsSendDto.setTime(DateUtil.dateToString(cale.getTime(), -1)+"号");
				smsSendDto.setNumber(total+"");
				sendSMSToAgency(smsSendDto);			
			}					
		}
	}
	
	/**
	 * 发送短信通知
	 */
	private void sendSMSToAgency(SMSSendDto smsSendDto){		
		String smsType = "normal";
		String smsFreeSignName = SMSUtil.SIGN_NAME[0];
		SMSTemplate sms = SMSTemplate.TEMPLATE_05;	
		Map<String, Object> smsParamMap = new HashMap<String, Object>();		
		smsParamMap.put("name", smsSendDto.getName());
		smsParamMap.put("time", smsSendDto.getTime());
		smsParamMap.put("number",smsSendDto.getNumber());	
		SMSSendReqInfo smsSendReq = new SMSSendReqInfo(smsType,
				smsFreeSignName, smsParamMap, smsSendDto.getRecNum(), sms);
		Map<String, Object> map = SMSUtil.sendSMS(smsSendReq);
		if (map.get("responseCode") == ResponseCode.SUCCESS.getCode()) {
			
			// 插入一条信息记录
			JSONObject jsonObject = JSONObject.fromObject(map
					.get("responseDesc"));
			jsonObject = JSONObject.fromObject(jsonObject
					.getString("alibaba_aliqin_fc_sms_num_send_response"));
			jsonObject = JSONObject.fromObject(jsonObject.getString("result"));				
			SMSSendInfo smsSendInfo = new SMSSendInfo(); 
			smsSendInfo.setSmsCode(sms.getTemplateID());
			smsSendInfo.setRecNum(smsSendDto.getRecNum());
			smsSendInfo.setSendCode(smsSendDto.getCode());
			//smsSendInfo.setUniqueFlag(uniqueFlag);	
			smsSendInfo.setSmsSendTime(new Date());				
			smsSendInfo.setSmsStatus("2");
			smsSendInfo.setSmsCode("SMS_60675021");
			smsSendInfo.setBizCode(jsonObject.getString("model"));
			smsSendInfoDao.insert(smsSendInfo);
		}
	}
	
	
	@Override
	public Object queryListBySharecode(ShareMemberInfo sa) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ShareMemberInfo> smList = shareMemberDao.queryListBySharecode(sa);
		int total = shareMemberDao.countShareMemberByShareCode(sa);
		map.put("rows", smList);
		map.put("total", total);
		return map;
	}


	@Override
	public Object queryShareCodeByUserId(Integer userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		ShareMemberInfo sm = shareMemberDao.queryShareCodeByUserId(userId);
		if (null != sm ) {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("shareMemberInfoList", sm);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}

	@Override
	public void txInsert(ShareMemberInfo memberInfo,
			Map<String, Object> map) {
		memberInfo.setRegisterTime(new Date());
		memberInfo.setIsPayStatus(0);
		int result = shareMemberDao.insert(memberInfo);
		if(result ==1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "插入失败");
		}
	}


	@Override
	public int txInsert(ShareMemberInfo memberInfo) {
		return shareMemberDao.insert(memberInfo);
	}

}
